package com.lb.poitl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Objects;

import javax.imageio.ImageIO;

import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import com.deepoove.poi.policy.AbstractRenderPolicy;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.render.WhereDelegate;

/**
 * @author jzhang
 * @version 1.0
 * @date 2022/6/7 17:05
 */
public class ImagePolicy extends AbstractRenderPolicy<List<String>> {
    @Override
    public void doRender(RenderContext<List<String>> context) throws Exception {
//        List<String> list = renderContext.getData();
        // anywhere delegate
        WhereDelegate where = context.getWhereDelegate();
        // any thing
        List<String> things = context.getThing();
        for (String thing : things) {
            // do 图片
            File file = new File(thing);
            if (!file.exists()) {
                continue;
            }
            FileInputStream stream = new FileInputStream(file);
            BufferedImage sourceImg = ImageIO.read(new FileInputStream(file));
            sourceImg.getType();
            System.out.println("width:" + sourceImg.getWidth());
            System.out.println("height:" + sourceImg.getHeight());
            where.addPicture(stream, XWPFDocument.PICTURE_TYPE_PNG, sourceImg.getWidth(), sourceImg.getHeight());
            IOUtils.closeQuietly(stream);
        }

        // clear
        clearPlaceholder(context, false);
    }
}
